package com.zmm.common.dict.enums;

import com.zmm.common.redis.key.CommonKey;

/**
 * @author 900045
 * @description:
 * @name RegionType
 * @date By 2021-07-22 20:43:58
 */
public enum RegionType {


	PROVINCE(CommonKey.DICT_REGION_PROVINCE, CommonKey.DICT_REGION_PROVINCE_V2, 1),

	CITY(CommonKey.DICT_REGION_CITY, CommonKey.DICT_REGION_CITY_V2, 2),

	DISTRICT(CommonKey.DICT_REGION_DISTRICT, CommonKey.DICT_REGION_DISTRICT_V2, 3),
	
	/** 版本2 镇，街道*/
	TOWN(CommonKey.DICT_REGION_TOWN_V2, 4);

	public CommonKey  redisKey;

	private CommonKey redisKeyV2;

	public int        level;

	RegionType(CommonKey redisKey, int level) {
		this.redisKey = redisKey;
		this.redisKeyV2 = redisKey;
		this.level = level;
	}

	RegionType(CommonKey redisKey, CommonKey redisKeyV2, int level) {
		this.redisKey = redisKey;
		this.redisKeyV2 = redisKeyV2;
		this.level = level;
	}

	public CommonKey getRedisKey() {
		return redisKey;
	}

	public CommonKey getRedisKey(RegionVersion regionVersion) {
		if (regionVersion == null || regionVersion == RegionVersion.V1) {
			return redisKey;
		}
		return redisKeyV2;
	}

	public static RegionType fetch(String type) {
		if (type == null) {
			return null;
		}
		for (RegionType regionType : RegionType.values()) {
			if (regionType.name().equalsIgnoreCase(type)) {
				return regionType;
			}
		}
		return null;
	}

	public static RegionType fetchByLevel(int level) {
		for (RegionType regionType : RegionType.values()) {
			if (regionType.level == level) {
				return regionType;
			}
		}
		return null;
	}
}
